package com.bianguo.android.iocdemo.view.activity;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.SparseArrayCompat;
import android.view.DisplayCutout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bianguo.android.eventbuslibrary.LiveDataBus;
import com.bianguo.android.eventbuslibrary.NetworkManager;
import com.bianguo.android.eventbuslibrary.annotation.Network;
import com.bianguo.android.eventbuslibrary.annotation.Subscribe;
import com.bianguo.android.eventbuslibrary.enums.NetworkState;
import com.bianguo.android.eventbuslibrary.enums.ThreadMode;
import com.bianguo.android.injectlibrary.annotations.ContentView;
import com.bianguo.android.injectlibrary.annotations.InjectView;
import com.bianguo.android.injectlibrary.annotations.InjectViews;
import com.bianguo.android.injectlibrary.annotations.OnClick;
import com.bianguo.android.injectlibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.event.EmptyEvent;
import com.bianguo.android.iocdemo.ui.UIHelper;
import com.bianguo.android.iocdemo.view.widget.DragBubbleView;

import java.util.List;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @InjectView(R.id.main_tv)
    private TextView tv;
    @InjectViews({R.id.main_test1, R.id.main_test2, R.id.main_test3,
            R.id.main_test4, R.id.main_test5})
    List<TextView> testTvs;
    @InjectView(R.id.main_unread_message)
    private DragBubbleView unreadMsgTv;
    @InjectView(R.id.filter_image)
    ImageView filterIv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addStatusViewWithColor(this, Color.WHITE);
        setAndroidNativeLightStatusBar(this, true);

        SparseArrayCompat<String> tests = new SparseArrayCompat<>();
        for (int i = 0; i < 5; i++) {
            tests.put(i, "item " + i);
        }
        for (int i = tests.size(); i >= 0; i--) {
            String value = tests.valueAt(i);
            int key = tests.keyAt(i);
            MLog.i("i = " + i + ", key = " + key + ", value = " + value);
        }
        UIHelper.getInstance(this);
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
//        Bitmap bitmap1 = ColorFilter.bitmapColorFilter(bitmap, ColorFilter.colormatrix_heibai);
//        filterIv.setImageBitmap(bitmap1);
    }

    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onResume() {
        super.onResume();
        tv.setText("你好世界");
        testTvs.get(0).setText("Hello IoC1");
        testTvs.get(1).setText("Hello IoC2");
        testTvs.get(2).setText("Hello IoC3");
        testTvs.get(3).setText("DrawActivity");
        testTvs.get(4).setText("PayActivity");

        unreadMsgTv.setText("23");
        NetworkManager.getDefault().register(this);
        LiveDataBus.getInstance().getChannel("MainActivity", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String string) {
                Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
        LayoutInflater inflater = LayoutInflater.from(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean hasDisplayCutout(Window window) {
        DisplayCutout displayCutout = null;
        View decorView = window.getDecorView();
        WindowInsets insets = decorView.getRootWindowInsets();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && insets != null) {
            displayCutout = insets.getDisplayCutout();
            if (displayCutout != null) {
                List<Rect> rects = displayCutout.getBoundingRects();
                if (rects != null && rects.size() > 0 && displayCutout.getSafeInsetTop() > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @OnClick({R.id.main_tv, R.id.main_test1, R.id.main_test2, R.id.main_test3,
            R.id.main_test4, R.id.main_test5})
    public void onClickIn(View view) {
        switch (view.getId()) {
            case R.id.main_tv:
                startActivity(new Intent(this, SplashActivity.class), ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
//                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, "basic");
//                startActivity(new Intent(this, SplashActivity.class), optionsCompat.toBundle());
                break;
            case R.id.main_test1:
                startActivity(new Intent(this, ParallaxActivity.class));
                break;
            case R.id.main_test2:
                startActivity(new Intent(this, PlayerActivity.class));
                break;
            case R.id.main_test3:
                startActivity(new Intent(this, MoveActivity.class));
                break;
            case R.id.main_test4:
                startActivity(new Intent(this, DrawActivity.class));
                break;
            case R.id.main_test5:
                startActivity(new Intent(this, PayActivity.class));
                break;
        }
    }

    @Subscribe(thread = ThreadMode.MAIN)
    private void onEvent(EmptyEvent event) {
        MLog.i("MainActivity onEvent");
    }

    @Network(state = NetworkState.WIFI)
    public void onNetworkChanged (NetworkState state) {
        MLog.i("onNetworkChanged ===");
    }

    @Override
    protected void onStop() {
        super.onStop();
        NetworkManager.getDefault().unRegister(this);
    }
}
